package swing.components.tablemodels;

import javax.swing.table.AbstractTableModel;

import container.WeaponTrait;
import container.Weapons;

/**
 * Weapon table component.
 * 
 * @author Marcus
 * 
 */
public class WeaponTableModel extends AbstractTableModel {

	/** Seerial ID */
	private static final long serialVersionUID = -4819745740422044921L;
	private Weapons weapons;
	private String[] headerName = new String[] { "Weaponname", "Code",
			"Accuracy", "DM", "Optimal Range", "Long Range", "Traits", "Cost" };

	/**
	 * Returns the number of the names in the header
	 * 
	 * @return number of names in the header
	 */
	public int getHeaderNameCount() {
		return this.headerName.length;
	}

	/**
	 * Constructor which creates this table on the basis of the passed
	 * {@link Weapons} object.
	 * 
	 * @param weapons
	 */
	public WeaponTableModel(Weapons setWeapons) {
		this.weapons = setWeapons;
	}

	@Override
	public int getColumnCount() {
		return getHeaderNameCount();
	}

	@Override
	public int getRowCount() {
		return this.weapons.getWeapons().size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return this.weapons.getWeapons().get(rowIndex).getWeaponName();
		case 1:
			return this.weapons.getWeapons().get(rowIndex).getWeaponCode();
		case 2:
			return Integer.valueOf(this.weapons.getWeapons().get(rowIndex)
					.getAccuracy());
		case 4:
			return Integer.valueOf(this.weapons.getWeapons().get(rowIndex)
					.getDamageMultiplicator());
		case 5:
			return Integer.valueOf(this.weapons.getWeapons().get(rowIndex)
					.getOptimalRange());
		case 6:
			return Integer.valueOf(this.weapons.getWeapons().get(rowIndex)
					.getLongRange());
		case 8:
			return getTraits(rowIndex);
		case 9:
			return Double.valueOf(this.weapons.getWeapons().get(rowIndex)
					.getWeaponCost());
		}

		return null;
	}

	private String getTraits(int weapon) {
		String traits = "";

		for (WeaponTrait trait : this.weapons.getWeapons().get(weapon)
				.getWeaponsTraits()) {
			traits = traits.concat(trait.getTraitCode()).concat(",");
		}

		if (!traits.isEmpty()) {
			traits = traits.substring(0, traits.length() - 1);
		}

		return traits;
	}

}
